#region Imports

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

#endregion

namespace IEC.ColumnOrdering.SP2007.Deployment
{

    /// <summary>
    /// Receives Feature management requests.
    /// </summary>
    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public abstract class ApplicationFeatureReceiver : SPFeatureReceiver
    {

        #region Methods - Static Member

        #region Methods - Instance Member - ApplicationFeatureReceiver Members

        /// <summary>
        /// Creates a feature element, using the given properties and feature element handler.
        /// </summary>
        /// <param name="properties">
        /// The <see cref="SPFeatureReceiverProperties"/> relating to an event.
        /// </param>
        /// <param name="featureElementHandler">
        /// The handler used to create the feature element.
        /// </param>
        protected virtual void CreateFeatureElement(
            SPFeatureReceiverProperties properties,
            FeatureElementHandler featureElementHandler)
        {
            SPWeb web = WebContextHelper.GetCurrentWeb(properties);

            bool originalUnsafeUpdateState = web.AllowUnsafeUpdates;

            try
            {
                web.AllowUnsafeUpdates = true;
                featureElementHandler.CurrentWeb = web;
                featureElementHandler.Create();
            }
            finally
            {
                web.AllowUnsafeUpdates = originalUnsafeUpdateState;
            }
        }

        /// <summary>
        /// Removes a feature element, using the given properties and feature element handler.
        /// </summary>
        /// <param name="properties">
        /// The <see cref="SPFeatureReceiverProperties"/> relating to an event.
        /// </param>
        /// <param name="featureElementHandler">
        /// The handler used to remove the feature element.
        /// </param>
        protected virtual void RemoveFeatureElement(
            SPFeatureReceiverProperties properties,
            FeatureElementHandler featureElementHandler)
        {
            SPWeb web = WebContextHelper.GetCurrentWeb(properties);

            bool originalUnsafeUpdateState = web.AllowUnsafeUpdates;

            try
            {
                web.AllowUnsafeUpdates = true;
                featureElementHandler.CurrentWeb = web;
                featureElementHandler.Remove();
            }
            finally
            {
                web.AllowUnsafeUpdates = originalUnsafeUpdateState;
            }
        }

        #endregion

        #endregion

    }

}
